package com.roger.myHome.config;

import com.alibaba.druid.support.http.StatViewServlet;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class DruidConfig {
    @Value("${druid.loginUsername}")
    private String username;

    @Value("${druid.loginPassword}")
    private String password;

    @Value("${druid.ip}")
    private String ip;

    @Bean
    public ServletRegistrationBean statViewServlet() {
        final ServletRegistrationBean<StatViewServlet> servletRegistrationBean
                = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
        // 配置druid参数
        Map<String, String> initParams = new HashMap<>(16);
        // 默认访问用户密码 admin:admin
        initParams.put("loginUsername", username);
        initParams.put("loginPassword", password);
        // 允许访问的ip
        initParams.put("allow", ip);
        // 黑名单ip，优先级比allow配置高
        initParams.put("deny", "");
        servletRegistrationBean.setInitParameters(initParams);
        return servletRegistrationBean;
    }

}
